fix(transcript): sum usage.iterations to count fallback attempts#170
Merged
Conversation
When a request is retried on a fallback model, the transcript's top-level usage only mirrors the final iteration; every billed attempt is listed in usage.iterations. Sum the iterations when there is more than one so token counts (and cost estimates) include the failed attempt as well. Single- or no-iteration usage objects are unaffected.
GuyMoses
approved these changes
Jul 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Token usage reported by the plugin can undercount what Claude Code itself reports (e.g. via
/usage). One confirmed cause: when a request is retried on a fallback model (observed in a real transcript as a Fable attempt followed by an Opus fallback under a singlerequestId), the transcript entry's top-levelmessage.usageonly mirrors the final iteration. Every billed attempt is listed inusage.iterations[].Real example: top-level showed
input=5607, output=698, while the summed iterations areinput=11214, output=700— a 50% input undercount for that call. Cache tokens are similarly undercounted.Fix
ReadTurnUsagenow decodesusage.iterationsand sums the token fields across iterations when there is more than one. With zero or one iteration (the normal case — the single iteration duplicates the top-level fields), behavior is byte-identical to before. The streaming keep-last-per-requestIddedup is unchanged; streamed duplicates repeat the same iterations array, so each API call is still counted once.Testing
go test ./...andgo vetclean.